package com.rj1101.hscrm.demo.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.rj1101.hscrm.demo.dao.AppointmentinfoDAO;
import com.rj1101.hscrm.demo.dao.CinfoDAO;
import com.rj1101.hscrm.demo.dao.FamilyinfoDAO;
import com.rj1101.hscrm.demo.dao.MedicalRecordsDAO;
import com.rj1101.hscrm.demo.dao.TreatmentDAO;
import com.rj1101.hscrm.demo.model.Appointmentinfo;
import com.rj1101.hscrm.demo.model.Cinfo;
import com.rj1101.hscrm.demo.model.Familyinfo;
import com.rj1101.hscrm.demo.model.MedicalRecords;
import com.rj1101.hscrm.demo.model.Treatment;
import com.rj1101.hscrm.demo.service.CustomerService;
import com.rj1101.hscrm.demo.util.Paging;

public class CustomerServiceImpl implements CustomerService {
	
	private CinfoDAO cDao;
	private AppointmentinfoDAO appDao;
	private MedicalRecordsDAO medDao;
	private FamilyinfoDAO fmDao;
	private TreatmentDAO trDao;
	

	public void setTrDao(TreatmentDAO trDao) {
		this.trDao = trDao;
	}

	public void setFmDao(FamilyinfoDAO fmDao) {
		this.fmDao = fmDao;
	}

	public void setcDao(CinfoDAO cDao) {
		this.cDao = cDao;
	}

	public void setAppDao(AppointmentinfoDAO appDao) {
		this.appDao = appDao;
	}

	public void setMedDao(MedicalRecordsDAO medDao) {
		this.medDao = medDao;
	}

	public boolean addCustomer(Cinfo customer) {
		// TODO Auto-generated method stub
		boolean s=false;
		try {
			cDao.save(customer);
			s=true;
		} catch (Exception e) {
			// TODO: handle exception
			s=false;
		}
		return s;
	}

	public boolean alterCustomer(Cinfo customer) {
		// TODO Auto-generated method stub
		boolean s=false;
		try {
			cDao.attachDirty(customer);
			s=true;
		} catch (Exception e) {
			// TODO: handle exception
			s=false;
		}
		return s;
	}

	public boolean delCustomer(Cinfo customer) {
		// TODO Auto-generated method stub
		boolean s=false;
		try {
			cDao.delete(customer);
			s=true;
		} catch (Exception e) {
			// TODO: handle exception
			s=false;
		}
		return s;
	}

	public Cinfo findCustomerById(int customerId) {
		// TODO Auto-generated method stub
		return cDao.findById(customerId);
	}

	public List<Cinfo> findByName(String name) {
		// TODO Auto-generated method stub
		return cDao.findByCname(name);
	}

	public Cinfo findByAppointId(int appointId) {
		// TODO Auto-generated method stub
		Integer cid=null;
		Appointmentinfo app=new Appointmentinfo();
		app=appDao.findById(appointId);
		cid=app.getCinfo().getCustomerId();
		return cDao.findById(cid);
	}

	public Cinfo findByCardId(String cardId) {
		// TODO Auto-generated method stub
		return (Cinfo) cDao.findByCardId(cardId);
	}

	public List<Cinfo> findByAppointDate(String start, String end) {
		// TODO Auto-generated method stub
		return  (List<Cinfo>)appDao.findByAppointDateTime(start, end);
	}

	public boolean addHistory(Cinfo customer, MedicalRecords records) {
		// TODO Auto-generated method stub
		customer=cDao.findById(customer.getCustomerId());
		System.out.println(customer);
		if(customer!=null){
			try {
				records.setCinfo(customer);
				medDao.save(records);
				return true;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		
		}else{
			
			return false;
		}
	
	}

	public boolean alterHistory(Cinfo customer, MedicalRecords records) {
		// TODO Auto-generated method stub
		if(customer!=null){
			try {
				medDao.attachDirty(records);
				return true;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		
		}else{
			
			return false;
		}
	
	}

	public boolean addFamily(Cinfo customer, Familyinfo records) {
		// TODO Auto-generated method stub
		if(cDao.findByProperty("customerId", customer.getCustomerId())!=null){
			return false;
		}else{
			fmDao.save(records);
			return true;
		}
	}

	public boolean alterFamily(Cinfo customer, Familyinfo records) {
		// TODO Auto-generated method stub
		if(cDao.findByProperty("customerId", customer.getCustomerId())!=null){
			return false;
		}else{
			fmDao.attachDirty(records);
			return true;
		}
	}

	public boolean delFamily(Familyinfo records) {
		try {
			// TODO Auto-generated method stub
			fmDao.delete(records);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	public List<Familyinfo> findFamilyByName(String name) {
		// TODO Auto-generated method stub
		List<Cinfo> cinfo=null;
		List<Familyinfo> fmInfo=null;
		cinfo=cDao.findByCname(name);
		for(int i=0;i<cinfo.size();i++){
			fmInfo.add(fmDao.findById(cinfo.get(i).getCustomerId()));
		}
		return fmInfo;
	}

	public List<Familyinfo> findFamilyByCardId(String cardId) {
		// TODO Auto-generated method stub
		List<Cinfo> cinfo=null;
		List<Familyinfo> fmInfo=null;
		cinfo=cDao.findByCardId(cardId);
		for(int i=0;i<cinfo.size();i++){
			fmInfo.add(fmDao.findById(cinfo.get(i).getCustomerId()));
		}
		return fmInfo;
	}

	public boolean addTreatment(Treatment records) {
		try {
			// TODO Auto-generated method stub
			trDao.save(records);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	public boolean alterTreatment(Treatment records) {
		try {
			// TODO Auto-generated method stub
			trDao.attachDirty(records);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	public boolean delTreatment(Treatment records) {
		// TODO Auto-generated method stub
		try {
			// TODO Auto-generated method stub
			trDao.delete(records);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	public List<Treatment> findTreatmentByName(String name) {
		// TODO Auto-generated method stub
		List<Cinfo> cinfo=null;
		List<Treatment> tre=null;
		cinfo=cDao.findByCname(name);
		for(int i=0;i<cinfo.size();i++){
			tre.add((Treatment) trDao.findByProperty("customerId", cinfo.get(i).getCustomerId()));
		}
		return tre;
	
	}

	public List<Treatment> findTreatmentById(int customerId) {
		// TODO Auto-generated method stub
		List<Treatment> tre=null;
		Cinfo customer = cDao.findById(customerId);
		tre=(List<Treatment>)trDao.findByProperty("cinfo.customerId", customer.getCustomerId());
		return tre;
	}

	public List<Treatment> findTreatmentByAppointId(int appointId) {
		// TODO Auto-generated method stub
		Cinfo cinfo=null;
		List<Treatment> tre=null;
		Appointmentinfo app=appDao.findById(appointId);
		cinfo=app.getCinfo();
		tre=tre=(List<Treatment>)trDao.findByProperty("customerId", cinfo.getCustomerId());
		return tre;
	}

	public List<Treatment> findTreatmentByDate(Cinfo customer, String start,
			String end) {
		// TODO Auto-generated method stub
		List<Treatment> tre=null;
		if(cDao.findByProperty("customerId", customer.getCustomerId())!=null){
			return tre;
		}else{
			tre=trDao.findByTreateDateTime(start, end);
			return tre;
		}
	}

	public List<Cinfo> findAllCinfo() {
		// TODO Auto-generated method stub
		return cDao.findAll();
	}

	public boolean delHistory(Cinfo customer, MedicalRecords records) {
		// TODO Auto-generated method stub
		if(customer!=null){
			try {
				medDao.delete(records);
				return true;
			} catch (Exception e) {
				// TODO: handle exception
				return false;
			}
		}else{
			return false;
		}
	}

	public List<MedicalRecords> findAllCustomerHistory() {
		// TODO Auto-generated method stub
		return medDao.findAll();
	}

	public List<Cinfo> findCustomerInfoByPage(String tbName,String tbGetFields,int pageSize, int pageIndex,
			String tbFileds,String orderDirection) {
		// TODO Auto-generated method stub
		return cDao.findCustomerInfoByPage( tbName, tbGetFields, pageSize,  pageIndex,
				 tbFileds, orderDirection);
	}


	public MedicalRecords findbyMrId(int mrId) {
		// TODO Auto-generated method stub
		return medDao.findById(mrId);
	}

	public List<Cinfo> pagingCustomerInfo(String tableName, Paging pager){
		return cDao.pagingCustomerInfo(tableName, pager);
	}

	public List<MedicalRecords> pageingHistory(String tableName, Paging pager) {
		// TODO Auto-generated method stub
		return medDao.pageingHistory(tableName, pager);
	}

	public ArrayList<Treatment> findAllTreatmentInfo() {
		// TODO Auto-generated method stub
		return (ArrayList<Treatment>) trDao.findAll();
	}

	public List<Treatment> pagingTreatmentInfo(String tableName, Paging pager){
		return trDao.pagingTreatmentInfo(tableName, pager);
	}

	

}
